package com.leapp.sk.view.im

import android.content.Context
import android.support.v4.content.ContextCompat
import android.support.v4.view.ViewCompat
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.HorizontalScrollView
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import com.hyphenate.util.DensityUtil
import com.leapp.sk.R

/**
 * @author  chaohaipeng  on 2018/7/13.
 */
class EaseEmojiconScrollTabBar : RelativeLayout {

    var mContext: Context? = null
    private var scrollView: HorizontalScrollView? = null
    private var tabContainer: LinearLayout? = null

    private val tabList = mutableListOf<ImageView>()
    private var itemClickListener: EaseScrollTabBarItemClickListener? = null

    constructor(context: Context) : super(context) {
        mContext = context
    }


    constructor(context: Context, attrs: AttributeSet?) : super(context, attrs) {
        mContext = context
        init(context, attrs)
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        init(context, attrs)
    }

    private fun init(context: Context, attrs: AttributeSet?) {
        val view = LayoutInflater.from(context).inflate(R.layout.ease_widget_emojicon_tab_bar, this)
        scrollView = view.findViewById(R.id.sv_tab_emoj_view)
        tabContainer = view.findViewById(R.id.ll_tab_emoj_container)
    }

    /**
     * add tab
     * @param icon
     */
    fun addTab(icon: Int) {
        val tabView = View.inflate(mContext, R.layout.ease_scroll_tab_item, null)
        val imageView = tabView.findViewById<ImageView>(R.id.iv_esti_emoj_icon)
        imageView.setImageResource(icon)
        val tabWidth = 60
        val imgParams = LinearLayout.LayoutParams(DensityUtil.dip2px(context, tabWidth.toFloat()), RelativeLayout.LayoutParams.MATCH_PARENT)
        imageView.layoutParams = imgParams
        tabContainer?.addView(tabView)
        tabList.add(imageView)
        val position = tabList.size - 1
        imageView.setOnClickListener {
                itemClickListener?.onItemClick(position)
        }
    }

    /**
     * remove tab
     * @param position
     */
    fun removeTab(position: Int) {
        tabContainer!!.removeViewAt(position)
        tabList.removeAt(position)
    }

    fun selectedTo(position: Int) {
        scrollTo(position)
        for (i in tabList.indices) {
            if (position == i) {
                tabList[i].setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_D9D9D9))
            } else {
                tabList[i].setBackgroundColor(ContextCompat.getColor(mContext, R.color.color_FFFFFF))
            }
        }
    }

    private fun scrollTo(position: Int) {
            var childCount = tabContainer?.childCount
        if (childCount!=null){
            if (position < childCount) {
                scrollView?.post(Runnable {
                    val mScrollX = tabContainer?.scrollX
                    val childX = ViewCompat.getX(tabContainer?.getChildAt(position)).toInt()
                    if(mScrollX!=null){
                        if (childX < mScrollX) {
                            scrollView?.scrollTo(childX, 0)
                            return@Runnable
                        }
                        val childWidth = tabContainer?.getChildAt(position)?.width
                        val hsvWidth = scrollView?.width
                        if (childWidth!=null&&hsvWidth!=null){
                            val childRight = childX + childWidth
                            val scrollRight = mScrollX + hsvWidth
                            if (childRight > scrollRight) {
                                scrollView!!.scrollTo(childRight - scrollRight, 0)
                                return@Runnable
                            }
                        }
                    }




                })
            }
        }

    }


    fun setTabBarItemClickListener(itemClickListener: EaseScrollTabBarItemClickListener) {
        this.itemClickListener = itemClickListener
    }


    interface EaseScrollTabBarItemClickListener {
        fun onItemClick(position: Int)
    }

}